﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Data.SqlClient;

namespace QL_NOI_TRU
{
    public partial class frmNhapTTNHANVIENReplace : DevExpress.XtraEditors.XtraForm
    {
        public frmNhapTTNHANVIENReplace()
        {
            InitializeComponent();
        }


        private void btThem_Click(object sender, EventArgs e)
        {
            string strr = "EXEC Insert_NHAN_VIEN @MA_KHOA ='" + cbbKhoa.Text.Substring(0, 2).Trim() + "',@MAPH ='" + cbbPhong.Text.Substring(0, 4).Trim() + "',@HOTEN=N'" + tbHotenNV.Text.ToString() + "',@SOCMND='" + tbsoCMND.Text.ToString().Trim() + "',@GIOITINH=N'" + cbbGTNV.Text.ToString() + "',@NGAYSINH='" + cbbngaysinhNV.Text.ToString() + "',@DIACHI=N'" + tbDiachiNV.Text.ToString() + "',@SODIENTHOAI='"+tbSoDTNV.Text.ToString().Trim()+"',@CHUCDANH=N'" + tbChucDanh.Text.ToString() + "',@CHUCVU=N'" + tbChucVu.Text.ToString() + "',@PASS='"+txtPass.Text.ToString().Trim()+"'";
            var kn = new Ketnoi(frmDangnhap.stringConn);
            kn.ExcuteQuery(strr);
            frmNhapTTNHANVIENReplace_Load(null, null);
        }

        private void btRefresh_Click(object sender, EventArgs e)
        {
            tbmaNV.Text = "";
            tbHotenNV.Text = "";
            tbChucDanh.Text = "";
            tbChucVu.Text = "";
            tbDiachiNV.Text = "";
            tbsoCMND.Text = "";
            tbSoDTNV.Text = "";
            cbbGTNV.SelectedIndex = -1;
            cbbKhoa.SelectedIndex = -1;
            cbbngaysinhNV.Text = "";
            cbbPhong.SelectedIndex = -1;
            frmNhapTTNHANVIENReplace_Load(null, null);
        }

        /// <summary>
        /// do du lieu vao grid + cbbKhoa
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmNhapTTNHANVIENReplace_Load(object sender, EventArgs e)
        {
            //đổ dữ liệu vào gridcontrol
            string n = "select MA_NV as 'Mã nhân viên',MAPH as 'Mã phòng',MA_KHOA as 'Mã khoa',HOTEN as 'Họ tên',SOCMND as 'Số CMND',GIOITINH as 'Giới tính',NGAYSINH as 'Ngày sinh',DIACHI as 'Địa chỉ',SODIENTHOAI as 'Số điện thoại',CHUCDANH as 'Chức danh',CHUCVU as 'Chức vụ'from NHAN_VIEN";
            var kn = new Ketnoi(frmDangnhap.stringConn);
            gridControl1.DataSource = kn.ExcuteQuery(n);
            //do du lieu vao cbbKhoa
            var maK = "select * from KHOA";
            SqlConnection conn = new SqlConnection(frmDangnhap.stringConn);
            SqlCommand cm = conn.CreateCommand();
            cm.CommandText = maK;
            conn.Open();
            SqlDataReader dr = cm.ExecuteReader();
            while (dr.Read())
            {
                var newVariable = cbbKhoa.Properties.Items.Add(String.Format("{0} | {1}", dr["MA_KHOA"], dr["TEN_KHOA"]));
            }
        }


        /// <summary>
        /// do du lieu vao cbbPhong
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbbKhoa_SelectedIndexChanged(object sender, EventArgs e)
        {
            string a = cbbKhoa.SelectedItem.ToString().Substring(0, 2);
            var maP = String.Format("select * from PHONG_TRUC where MA_KHOA = '{0}'", a);
            SqlConnection conn = new SqlConnection(frmDangnhap.stringConn);
            SqlCommand cm = conn.CreateCommand();
            cm.CommandText = maP;
            conn.Open();
            SqlDataReader dr = cm.ExecuteReader();
            while (dr.Read())
            {
                var newVariable = cbbPhong.Properties.Items.Add(String.Format("{0} | {1}", dr["MAPH"],dr["TEN_PHONG"]));
            }
        }

        private void cbbKhoa_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
                cbbPhong.Focus();
        }

        private void cbbPhong_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
                tbHotenNV.Focus();
        }

        private void tbHotenNV_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
                tbsoCMND.Focus();
        }

        private void tbsoCMND_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
                cbbGTNV.Focus();
        }

        private void cbbGTNV_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
                cbbngaysinhNV.Focus();
        }

        private void cbbngaysinhNV_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
                tbDiachiNV.Focus();
        }

        private void tbDiachiNV_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
                tbSoDTNV.Focus();
        }

        private void tbSoDTNV_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
                tbChucDanh.Focus();
        }

        private void tbChucDanh_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
               tbChucVu.Focus();
        }

        private void tbChucVu_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
                txtPass.Focus();
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            QL_NOI_TRU.DataLayers.KetNoiHelper hp = new DataLayers.KetNoiHelper();
            OpenFileDialog dlg = new OpenFileDialog();
            //   dlg.Filter = "JPG Files(*.JPG)|*.JPG Files(*.GIF)|*.GIF";
            SqlConnection conn = new SqlConnection(frmDangnhap.stringConn);
            if (dlg.ShowDialog(this) == DialogResult.OK)
            {
                hp.StorePicture(dlg.FileName);
                  //   pictureBox1.Image = Image.FromFile(dlg.FileName);
            }
        }

    }
}